package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.commons.SystemInfo;
import com.suineg.pms.entity.fee.FeeScal;
import com.suineg.pms.service.commoms.SystemInfoService;
import com.suineg.pms.service.fee.FeeScaleService;
import com.suineg.pms.util.StringUtils;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 交费基准 管理  保存拦截器
 *@author Suine
 */
public class FeeScaleIntercepter extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private FeeScaleService feeScaleService = AppConfig.getInstance().getWac().getBean(FeeScaleService.class);
	private SystemInfoService  systemInfoService =  AppConfig.getInstance().getWac().getBean(SystemInfoService.class);

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		String id = mRowData.get("id");
		String name = mRowData.get("name");
		String type = mRowData.get("type");
		
		FeeScal feeScal = null;
		
		if(!StringUtils.isEmpty(id)){		//修改操作
			feeScal = feeScaleService.get(Long.valueOf(id));
			if(!name.equals(feeScal.getName())){				//修改名字时间判断 重复性
				checkName(rrequest, name);
			}
			
			if(!Long.valueOf(type).equals(feeScal.getType().getId())){
				checkType(rrequest, type);
			}
		}else{
			checkName(rrequest, name);
			checkType(rrequest, type);
		}
		
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}
	
	public void checkName(ReportRequest rrequest,String name){
		
		FeeScal feeScal = null;
		feeScal = feeScaleService.findByName(name);
		
		if(feeScal != null){
			rrequest.getWResponse().getMessageCollector().warn("此“基准名: " + name + "”已存在！", false, Consts.STATECODE_FAILED);
		}
	}
	
	public void checkType(ReportRequest rrequest,String type){
		SystemInfo systemInfo = systemInfoService.get(Long.valueOf(type));
		List<FeeScal> list = systemInfo.getFeeScaleList();
		if(list != null && !list.isEmpty()){
			rrequest.getWResponse().getMessageCollector().warn("此“基准类型: " + systemInfo.getName() + "”已存在！", false, Consts.STATECODE_FAILED);
		}
	}
}

